package jmeterBusiness;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import impl.TestServerBusinessImpl;
import models.MyConfig;
import models.ReturnJson;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.Objects;

/**
 * Created by yangxianming on 2019/1/28.
 */
public class ScriptManager implements TestServerBusinessImpl {
    @Override
    public String doIt(HttpServletRequest request, MyConfig myConfig) {
        //初始化返回对象
        ReturnJson returnJson = new ReturnJson();
        //读取请求
        String action = request.getParameter("action");
        //获取系统名字 及配置一些参数
        String splitMark = myConfig.getSplitMark("splitMark");
        String appPath = myConfig.getPropertiesConfig().getProperties().getProperty("appPath");
        String jmeterScriptDir = appPath + splitMark + "jmeterScript" + splitMark;


        //增加项目
        if (Objects.equals("addProject", action)){
            String projectName = request.getParameter("projectName");
            String newProjectDir = jmeterScriptDir + splitMark + projectName;
            File file = new File(newProjectDir);
            if (file.mkdir()){
                returnJson.setResult("ok");
            }else {
                returnJson.setErrorMsg("创建项目目录失败");
                returnJson.setApiStatus(0);
            }
            return returnJson.toString();
        }

        //删除项目（目录下无文件）
        if (Objects.equals("delProject", action)){
            String projectName = request.getParameter("projectName");
            String delProjectDir = jmeterScriptDir + splitMark + projectName;
            File file = new File(delProjectDir);
            if (file.delete()){
                returnJson.setResult("ok");
            }else {
                returnJson.setErrorMsg("找不到项目目录或者不是一个空的项目目录无法移除，请联系管理员小明");
                returnJson.setApiStatus(0);
            }
            return returnJson.toString();
        }

        //获取项目和脚本目录
        if (Objects.equals("getList", action)){
            //返回jmeterScript下的二级目录。一级是项目目录，二级是jmx和其他文件
            File file = new File(jmeterScriptDir);
            JSONObject jmeterScriptList = new JSONObject();
            try {
                for (File file1 : file.listFiles()){
                    if (file1.isDirectory()){
                        //得到项目jmeterScript下一层的“项目目录”
                        String scriptDir = file1.getName();
                        jmeterScriptList.put(scriptDir, new JSONArray());

                        //循环脚本项目目录，找出所有的jmx文件
                        File file2 = new File(jmeterScriptDir + scriptDir);
                        for (File file3 : file2.listFiles()){
                            String scriptName = file3.getName();
                            jmeterScriptList.getJSONArray(scriptDir).add(scriptName);
                        }
                    }
                }
                returnJson.setResult(jmeterScriptList);
            }catch (Exception e){
                returnJson.setApiStatus(0);
                returnJson.setErrorMsg("读取项目目录和脚本失败。" + e.getMessage());
            }
            return returnJson.toString();
        }


        return returnJson.toString();
    }

}
